import {view, binder} from "adajs";
import InputService from "./state.js";
import BaseField from "./../field";

@view({
    className: "field-input",
    template: "./template.html",
    style: "./style.scss",
    dataset: {
        service: InputService
    }
})
class Input extends BaseField {
    getValue() {
        return this.getCurrentState().active.id;
    }

    _getValue() {
        return this.finder('input').getElement().value;
    }

    setValue(value) {
        return this.commit('setValue', value);
    }

    showError(info) {
        return this.commit("showError", info);
    }

    hideError() {
        return this.commit("hideError");
    }

    getName() {
        return this.getCurrentState().name;
    }

    @binder("search")
    search() {
        this.commit("search", this._getValue());
    }

    @binder("active")
    active({item}) {
        this.commit("active", item).then(() => {
            this.dispatchEvent('selected', this.getCurrentState().active);
        });
    }

    check() {
        return this.commit('check', this._getValue()).then(() => this.getCurrentState().error === false);
    }
}

export default Input;